package com.gxar.marketdelivery.domain.repo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxar.marketdelivery.domain.enums.DeliveryTarget;
import com.gxar.marketdelivery.infra.dao.entity.DeliveryRuleEntity;
import com.gxar.marketdelivery.infra.dao.mapper.DeliveryRuleMapper;
import com.gxar.marketdelivery.interfaces.web.result.IResponseStatusMsg;
import com.gxar.marketdelivery.shared.consts.DBConsts;
import com.gxar.marketdelivery.shared.utils.AssertUtils;
import com.gxar.marketdelivery.shared.utils.CollectionUtils;
import com.gxar.marketdelivery.shared.utils.DBUtils;
import com.gxar.marketdelivery.shared.utils.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author nice
 * @version 1.0
 * @created 2023/7/23.
 */
@Service
public class DeliveryRuleRepository {

    @Autowired
    protected DeliveryRuleMapper mapper;

    public void create(DeliveryRuleEntity entity) {
        entity.setGmtModify(DateTimeUtils.now());
        int ret = mapper.insert(entity);
        AssertUtils.isTrue(DBUtils.isSuccess(ret), IResponseStatusMsg.APIEnum.DB_ERROR, "新增投放规则失败");
    }

    public void deleteByTargetId(DeliveryTarget deliveryTarget, Long targetId) {
        UpdateWrapper<DeliveryRuleEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(DeliveryRuleEntity.FIELD_STATUS, DBConsts.STATUS_UNAVAILABLE);
        updateWrapper.eq(DeliveryRuleEntity.FIELD_TARGET_ID, targetId);
        updateWrapper.eq(DeliveryRuleEntity.FIELD_DELIVERY_TARGET, deliveryTarget.getCode());
        mapper.update(getByTargetId(deliveryTarget, targetId), updateWrapper);
    }

    public DeliveryRuleEntity getByTargetId(DeliveryTarget deliveryTarget, Long targetId) {
        QueryWrapper<DeliveryRuleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(DeliveryRuleEntity.FIELD_TARGET_ID, targetId);
        queryWrapper.eq(DeliveryRuleEntity.FIELD_DELIVERY_TARGET, deliveryTarget.getCode());
        queryWrapper.eq(DeliveryRuleEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        return mapper.selectOne(queryWrapper);
    }

    public Map<Long, DeliveryRuleEntity> multiGet(DeliveryTarget deliveryTarget, List<Long> targetIds) {
        if (CollectionUtils.isEmpty(targetIds)) {
            return Collections.emptyMap();
        }
        QueryWrapper<DeliveryRuleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(DeliveryRuleEntity.FIELD_TARGET_ID, targetIds);
        queryWrapper.eq(DeliveryRuleEntity.FIELD_DELIVERY_TARGET, deliveryTarget.getCode());
        queryWrapper.eq(DeliveryRuleEntity.FIELD_STATUS, DBConsts.STATUS_AVAILABLE);
        List<DeliveryRuleEntity> ruleEntities = mapper.selectList(queryWrapper);
        return CollectionUtils.toMap(ruleEntities, DeliveryRuleEntity::getTargetId);
    }
}
